﻿using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Pdf.HtmlConverter;

namespace HtmlToPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            PdfPageSettings setting = new PdfPageSettings();
            setting.Size = PdfPageSize.A1;

            String url = "https://www.e-iceblue.com/Introduce/word-for-net-introduce.html";
            //doc.LoadFromHTML(url, false, true, true, setting);
            //Spire.Pdf.HtmlConverter.LoadHtmlType.URL
            string result = "HtmlToPdf.pdf";
            //Save pdf file        
            doc.SaveToFile(result);
            doc.Close();
            System.Diagnostics.Process.Start(result);

        }
        public byte[] Url_To_Byte(String filePath)
        {
            //第一步：读取图片到byte数组
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath);

            byte[] bytes;
            using (Stream stream = request.GetResponse().GetResponseStream())
            {
                using (MemoryStream mstream = new MemoryStream())
                {
                    int count = 0;
                    byte[] buffer = new byte[1024];
                    int readNum = 0;
                    while ((readNum = stream.Read(buffer, 0, 1024)) > 0)
                    {
                        count = count + readNum;
                        mstream.Write(buffer, 0, readNum);
                    }
                    mstream.Position = 0;
                    using (BinaryReader br = new BinaryReader(mstream))
                    {
                        bytes = br.ReadBytes(count);
                    }
                }
            }
            return bytes;


        }
    }
}
